package com.bjpowerndoe.ylb.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowerndoe.ylb.domain.FinanceAccountPO;
import com.bjpowerndoe.ylb.domain.ProductInfoPO;
import com.bjpowerndoe.ylb.map.BidUserMap;
import com.bjpowerndoe.ylb.map.BidUserMap;
import com.bjpowerndoe.ylb.mapper.FinanceAccountMapper;
import com.bjpowerndoe.ylb.mapper.ProductInfoMapper;
import com.bjpowerndoe.ylb.web.model.dto.ProductDTO;
import com.bjpowerndoe.ylb.web.model.dto.ProductDetailDTO;
import com.bjpowerndoe.ylb.web.service.inter.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 *
 */
@RequiredArgsConstructor
@Service
public class ProductServiceImpl implements ProductService {

    private final ProductInfoMapper productInfoMapper;
    private final FinanceAccountMapper accountMapper;

    @Override
    public IPage<ProductInfoPO> queryPageByType(Integer productType, Integer pageNo, Integer pageSize) {
        QueryWrapper<ProductInfoPO> condition = new QueryWrapper<>();
        condition.eq("product_type",productType);
        IPage<ProductInfoPO> page = new Page<>(pageNo,pageSize);
        return productInfoMapper.selectPage(page, condition);
    }

    @Override
    public ProductDetailDTO queryDetailByProductId(Integer productId, Integer uid) {

        //查询理财产品
        ProductInfoPO productInfoPO = productInfoMapper.selectById(productId);

        //查询投资这个产品的记录
        List<BidUserMap> bidList = productInfoMapper.selectRecentlyBidInfo(productId, 0, 5);

        //查询用户资金
        BigDecimal accountMoney = new BigDecimal("-1");
        if( uid != null && uid > 0 ){
            FinanceAccountPO accountPO = accountMapper.selectOne(new QueryWrapper<FinanceAccountPO>().eq("uid", uid));
            if( accountPO != null){
                accountMoney = accountPO.getAvailableMoney();
            }
        }
        ProductDetailDTO dto  = new ProductDetailDTO();
        dto.setProductDTO(BeanUtil.copyProperties(productInfoPO,ProductDTO.class));
        dto.setBidList(bidList);
        dto.setAccountMoney(accountMoney);
        return dto;
    }
}
